###############################
# Figures A2 and A3
###############################

rm(list=ls())

# load packages 
library(foreign)
library(lmtest)
library(sandwich)
library(stargazer)
library(msm)
library("readxl")
library(cregg)
library("ggplot2")
library(gridExtra)
library(readstata13)

##############################
# Prepare data
##############################

# load data
load("replication_data.Rdata")
names(d)

#######################################
# AMCE by Bureaucrat Gender Q1
#######################################

amces4a <- cj(d, outcome_q1 ~ speed_eng + theft_eng  + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce", by = ~gender_eng)[c(1:4,14:17),]
amces4a$feature = c("Speed corruption", "Speed corruption", "Theft corruption", "Theft corruption","Speed corruption", "Speed corruption", "Theft corruption", "Theft corruption")
diff_amces4a <- cj(d, outcome_q1 ~ speed_eng + theft_eng + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce_diff", by = ~gender_eng)[7:8,]
diff_amces4a$feature = c("Speed corruption", "Theft corruption")
plot4a = plot(rbind(amces4a, diff_amces4a)) + ggplot2::facet_wrap(~BY, ncol = 3L) + ggtitle("") + theme(plot.title = element_text(hjust = 0.5)) + geom_point(size = 3) + scale_y_discrete(labels=c("(Speed corruption)"="SPEED CORRUPTION","(Theft corruption)"="THEFT CORRUPTION")) + theme(text = element_text(size = 25))

ggsave('figureA3.pdf', plot4a, width = 20, height = 10)

#######################################
# AMCE by Bureaucrat Gender Q2
#######################################

amces4b <- cj(d, outcome_q2 ~ speed_eng + theft_eng  + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce", by = ~gender_eng)[c(1:4,14:17),]
amces4b$feature = c("Speed corruption", "Speed corruption", "Theft corruption", "Theft corruption","Speed corruption", "Speed corruption", "Theft corruption", "Theft corruption")
diff_amces4b <- cj(d, outcome_q2 ~ speed_eng + theft_eng + partyid_eng + age_eng + education_eng, id = ~idnum, estimate = "amce_diff", by = ~gender_eng)[7:8,]
diff_amces4b$feature = c("Speed corruption", "Theft corruption")
plot4b = plot(rbind(amces4b, diff_amces4b)) + ggplot2::facet_wrap(~BY, ncol = 3L) + ggtitle("") + theme(plot.title = element_text(hjust = 0.5)) + geom_point(size = 3) + scale_y_discrete(labels=c("(Speed corruption)"="SPEED CORRUPTION","(Theft corruption)"="THEFT CORRUPTION")) + theme(text = element_text(size = 25))

ggsave('figureA4.pdf', plot4b, width = 20, height = 10)

